<template>
  <div class="visa-body">
    <!-- header部分 -->
    <nav-title title="审批链加签" />
    <div class="container clearfix">
      <div class="search clearfix">
        <img class="search-img" src="./images/user.png" alt="" @click="searchVisa">&nbsp;
        <input class="search-input" type="text" placeholder="请输入搜索关键字" v-model="searchText">
        <img class="search-img" src="./images/searchicon.png" alt="" @click="searchVisa">
      </div>
    </div>
    <div v-for="(visa,index) in visaList" class="visa-user">
      <input type="checkbox" name="visaBox" :value="visa.emplid">
      <label class="name">{{visa.nameDisplay}}</label>
      <label>({{visa.deptDescr}})</label>
    </div>

    <div class="visa-btn">
      <button @click="visaUser">加签</button>
    </div>
  </div>
</template>

<script>
import {
  getVisa,
  getCompensateApprove,
  getFormalApprove,
  getWastageApprove,
  getRecruitmentApproveDrft
} from '../../api/api'
import { json2Form } from '../../util/http'

export default {
  components: {},
  mounted() {
    this.cWfType = this.$route.params.c_WF_TYPE
    this.cTransactionNbr = this.$route.params.c_TRANSACTION_NBR
    this.cComments = this.$route.params.c_COMMENTS
    this.cLink = this.$route.params.c_Link
    this.formalParams = this.$route.params.formalParams
    this.wastageParams = this.$route.params.wastageParams
    this.recruitmentParams = this.$route.params.recruitmentParams
  },
  data() {
    return {
      searchText: '',
      visaList: [],

      //按钮点击的参数
      cWfType: '',
      cTransactionNbr: '',
      cComments: '',
      cLink: '',
      formalParams: {},
      wastageParams: {},
      recruitmentParams: {},
    }
  },
  methods: {
    //获取加签人员列表
    searchVisa() {
      if (this.searchText == '') {
        this.$vux.alert.show({
          title: '警告',
          content: '请输入'
        })

        return
      }
      let params = {
        queryValue: this.searchText
      }
      getVisa(params)
        .then(res => {
          if (res && res.retCode == '000000') {
            this.visaList = res.retData
          }
        })
        .catch(res => {
          this.$vux.alert.show({
            title: '警告',
            content: '网络异常，请稍后重试'
          })
        })
    },
    visaUser() {
      let id = document.getElementsByName('visaBox')
      let value = []
      for (var i = 0; i < id.length; i++) {
        if (id[i].checked) value.push(id[i].value)
      }
      let params = {
        c_WF_TYPE: this.cWfType,
        c_TRANSACTION_NBR: this.cTransactionNbr, //流程编号
        c_ACTION_TYPE: 'AddStep',
        c_COMMENTS: this.cComments,
        addUserids: value
      }

      if (value == '') {
        this.$vux.alert.show({
          title: '警告',
          content: '请选择人员'
        })
        return
      }
      if (this.formalParams) {
        this.formalParams.addUserIds = value
        getFormalApprove(json2Form(this.formalParams))
          .then(res => {
            if (res && res.retCode === '000000') {
              let _this = this
              this.$vux.alert.show({
                title: '成功',
                content: '操作成功',
                onHide() {
                  _this.$router.push({
                    name: 'FormalShow',
                    query: {
                      cTransactionNbr: this.formalParams.cTransactionNbr
                    }
                  })
                }
              })
            } else {
              this.$vux.alert.show({
                title: '',
                content: res.retMsg
              })
            }
          })
          .catch(res => {
            this.$vux.alert.show({
              title: '警告',
              content: '网络异常，请稍后重试'
            })
          })
      } else if (this.wastageParams) {
        this.wastageParams.addUserIds = value
        getWastageApprove(json2Form(this.wastageParams))
          .then(res => {
            if (res && res.retCode === '000000') {
              let _this = this
              this.$vux.alert.show({
                title: '成功',
                content: '操作成功',
                onHide() {
                  _this.$router.push({
                    name: 'WastageShow',
                    query: {
                      cTransactionNbr: this.wastageParams.cTransactionNbr
                    }
                  })
                }
              })
            } else {
              this.$vux.alert.show({
                title: '',
                content: res.retMsg
              })
            }
          })
          .catch(res => {
            this.$vux.alert.show({
              title: '警告',
              content: '网络异常，请稍后重试'
            })
          })
      } else if (this.recruitmentParams) {
        this.recruitmentParams.addUserIds = value;
        const cTransactionNbr = this.recruitmentParams.cTransactionNbr;
        let _this = this;
        getRecruitmentApproveDrft(json2Form(this.recruitmentParams))
          .then(res => {
            if (res && res.retCode === '000000') {
              this.$vux.alert.show({
                title: '成功',
                content: '操作成功',
                onHide() {
                  _this.$router.push({
                    name: 'Employment',
                    query: {
                      cTransactionNbr,
                      approved: '1',
                    }
                  })
                }
              })
            } else {
              this.$vux.alert.show({
                title: '',
                content: res.retMsg
              })
            }
          })
          .catch(res => {
            this.$vux.alert.show({
              title: '警告',
              content: '网络异常，请稍后重试'
            })
          })
      } else {
        getCompensateApprove(json2Form(params))
          .then(res => {
            if (res && res.retCode === '000000') {
              let _this = this
              this.$vux.alert.show({
                title: '成功',
                content: '操作成功',
                onHide() {
                  //跳到已办对应的流程
                  if (_this.cLink == 'Formal') {
                    _this.$router.push({
                      name: 'FormalShow',
                      query: {
                        cTransactionNbr: _this.cTransactionNbr
                      }
                    })
                  } else if (_this.cLink == 'Wastage') {
                    _this.$router.push({
                      name: 'WastageShow',
                      query: {
                        cTransactionNbr: _this.cTransactionNbr
                      }
                    })
                  } else {
                    _this.$router.push({
                      name: _this.cLink,
                      query: {
                        cTransactionNbr: _this.cTransactionNbr,
                        approved: '1'
                      }
                    })
                  }
                }
              })
            } else {
              this.$vux.alert.show({
                title: '',
                content: res.retMsg
              })
            }
          })
          .catch(res => {
            this.$vux.alert.show({
              title: '警告',
              content: '网络异常，请稍后重试'
            })
          })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.visa-body {
  height: 100%;
  /* background-color: #f2f2f2; */
  .search {
    text-align: left;
    .search-img {
      margin-left: 30px;
    }
    .search-input {
      width: 60%;
    }
  }
}
.container {
  background-color: #f2f2f2;
  margin-bottom: 20px;
  height: 40px;
  line-height: 40px;
}

.visa-user {
  padding: 10px 10px 10px 10px;
  margin: 0px 10px 10px 10px;
  text-align: left;
  background-color: #f2f2f2;
  border-radius: 4px;
}
.visa-user img {
  vertical-align: text-top;
}

.name {
  font-weight: bold;
}

.visa-btn {
  width: 100%;
  margin: 0 auto;
  color: #fff;
  position: fixed;
  bottom: 15px;
  text-align: center;
}

.visa-btn button {
  color: white;
  font-size: 16px;
  font-weight: 900;
  line-height: 40px;
  width: 250px;
  height: 40px;
  background-color: #1caafe;
  border-radius: 4px;
}
</style>
